﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TugasAkhir.Helpers;
using TugasAkhir.Models;
using TugasAkhir.Objects;
using TugasAkhir.Objects.JsonObject;

namespace TugasAkhir
{
    public partial class Analisis : System.Web.UI.Page
    {
        private DataSet dsMean;
        private DosenObject dosen;
        private bool multipleTahun;

        private HitungAnalisis ha1;
        private HitungAnalisis ha2;
        private HitungAnalisis ha3;

        private List<string> tahun;

        private void CheckRole()
        {
            if (int.Parse(Session["id_role"].ToString().Substring(2)) == 2)
            {
                this.liPertanyaan.Attributes.Remove("hidden");
                this.liPengaturanPertanyaan.Attributes.Remove("hidden");
                this.liAnalisis.Attributes.Remove("hidden");
                this.liPengaturanRekomendasi.Attributes.Remove("hidden");
                this.headerMenuRekomendasi.InnerHtml = "Rekomendasi";
            }
            else if (int.Parse(Session["id_role"].ToString().Substring(2)) == 3)
            {
                this.liAnalisis.Attributes.Remove("hidden");
                this.liPengaturanRekomendasi.Attributes.Remove("hidden");
                this.headerMenuRekomendasi.InnerHtml = "Rekomendasi";
            }
            else
            {
                this.liDaftarRekomendasi.Attributes.Remove("hidden");
                this.liAnalisis.Attributes.Remove("hidden");
                this.liPengaturanRekomendasi.Attributes.Remove("hidden");
            }
        }

        private void CheckSession()
        {
            if (Session["id_role"] != null && Session["id_role"].ToString() != "RL01" && Session["id_role"].ToString().StartsWith("RL"))
            {
                DosenModel.GetDosenDetail(Session["username"].ToString(), out this.dosen);
                this.litName.Text = dosen.NamaDosen;
            }
            else
            {
                Response.Redirect("/Login.aspx");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckSession();
            CheckRole();
            if (!Page.IsPostBack)
            {
                this.InitDropDownTahun();
            }
        }

        private void InitDropDownTahun()
        {
            var defaultItem = new ListItem()
            {
                Text = "-- pilih salah satu --",
                Value = ""
            };

            this.ddlTahun.Items.Add(defaultItem);

            var data = new DataSet();
            bool flag = RekomendasiModel.GetTahunPengambilanDosen(out data);

            if (flag)
            {
                foreach (DataRow item in data.Tables[0].Rows)
                {
                    this.ddlTahun.Items.Add(new ListItem { Text = item[0].ToString(), Value = item[0].ToString() });
                }
            }

            if (this.ddlPeriode.Items.Count == 0)
                this.ddlPeriode.Items.Add(defaultItem);

            if (this.ddlMatakuliah.Items.Count == 0)
                this.ddlMatakuliah.Items.Add(defaultItem);
        }

        protected void ddlTahun_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.hfTahun.Value = this.ddlTahun.SelectedValue;
            this.hfPeriode.Value = "";
            this.hfMatakuliah.Value = "";

            if (this.ddlTahun.SelectedValue == "")
            {
                var defaultItem = new ListItem()
                {
                    Text = "-- pilih salah satu --",
                    Value = ""
                };

                this.ddlPeriode.Items.Clear();
                this.ddlPeriode.Items.Add(defaultItem);
            }
            else
            {
                this.InitDropDownPeriode();
            }
        }

        private void InitDropDownPeriode()
        {
            var defaultItem = new ListItem()
            {
                Text = "-- pilih salah satu --",
                Value = ""
            };

            this.ddlPeriode.Items.Clear();
            this.ddlPeriode.Items.Add(defaultItem);

            this.ddlMatakuliah.Items.Clear();
            this.ddlMatakuliah.Items.Add(defaultItem);

            var data = new DataSet();
            bool flag = RekomendasiModel.GetPeriodePengambilanDosen(this.hfTahun.Value, out data);

            if (flag)
            {
                foreach (DataRow item in data.Tables[0].Rows)
                {
                    this.ddlPeriode.Items.Add(new ListItem { Text = item[0].ToString(), Value = item[0].ToString() });
                }
            }
            else
            {
                this.ddlTahun.SelectedValue = "";
            }
        }

        protected void ddlPeriode_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.hfPeriode.Value = this.ddlPeriode.SelectedValue;
            this.hfMatakuliah.Value = "";
            if (this.ddlPeriode.SelectedValue == "")
            {
                var defaultItem = new ListItem()
                {
                    Text = "-- pilih salah satu --",
                    Value = ""
                };

                this.ddlMatakuliah.Items.Clear();
                this.ddlMatakuliah.Items.Add(defaultItem);
            }
            else
            {
                this.InitDropDownMatakuliah();
            }
        }

        private void InitDropDownMatakuliah()
        {
            var defaultItem = new ListItem()
            {
                Text = "-- pilih salah satu --",
                Value = ""
            };

            this.ddlMatakuliah.Items.Clear();
            this.ddlMatakuliah.Items.Add(defaultItem);

            var data = new DataSet();
            bool flag = false;
            if (Session["id_role"].ToString() == "RL03")
            {
                flag = RekomendasiModel.GetMatakuliahPengambilanDosenUmum(this.hfTahun.Value, this.hfPeriode.Value, this.dosen.IdDosen, out data);
            }
            else if (Session["id_role"].ToString() == "RL02")
            {
                flag = RekomendasiModel.GetMatakuliahPengambilanDosenKajur(this.hfTahun.Value, this.hfPeriode.Value, out data);
            }
            else
            {
                var hasilQuery = new DataSet();
                PenggunaModel.GetIdLab(this.dosen, out hasilQuery);

                string idLab = hasilQuery.Tables[0].Rows[0][0].ToString();
                flag = RekomendasiModel.GetMatakuliahPengambilanDosenKalab(this.hfTahun.Value, this.hfPeriode.Value, idLab, out data);
            }

            if (flag)
            {
                foreach (DataRow item in data.Tables[0].Rows)
                {
                    this.ddlMatakuliah.Items.Add(new ListItem { Text = item[1].ToString(), Value = item[0].ToString() });
                }
            }
            else
            {
                this.ddlPeriode.SelectedValue = "";
            }
        }

        protected void ddlMatakuliah_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.hfMatakuliah.Value = this.ddlMatakuliah.SelectedValue;
        }

        protected void btTampilNilai_Click(object sender, EventArgs e)
        {
            if (this.hfTahun.Value == "" || this.hfMatakuliah.Value == "") return;

            bool flag = AnalisisModel.GetMean(this.hfTahun.Value, this.hfPeriode.Value, this.hfMatakuliah.Value, out this.dsMean);

            if (flag)
            {
                this.ShowChart();
                this.gvPertanyaan.Visible = false;
            }
        }

        private void ShowChart()
        {
            var rerata = new List<double>();
            var kategori = new List<string>();

            foreach (DataRow item in this.dsMean.Tables[0].Rows)
            {
                rerata.Add(Math.Round(double.Parse(item[1].ToString()), 2, MidpointRounding.ToEven));
                kategori.Add(item[0].ToString());
            }

            string script = String.Format("var chart = c3.generate({{ bindto: '#chart-query0', data: {{ columns: [ ['rata-rata'{0}] ] }}, axis: {{ x: {{ type: 'category', categories: [{1}] }} }} }}); $('#bagianchart0').removeAttr('hidden'); $('#bagianchart1').attr('hidden', 'hidden'); $('#bagianchart2').attr('hidden', 'hidden');", this.PrintRerata(rerata), this.PrintCategory(kategori));

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowChart", script, true);
        }

        private string PrintRerata(List<double> rerata)
        {
            string result = string.Empty;

            foreach (double r in rerata)
            {
                result += " , " + r.ToString();
            }
            return result;
        }

        private string PrintCategory(List<string> kategori)
        {
            string result = string.Empty;

            foreach (string s in kategori)
            {
                result += " , '" + s + "'";
            }
            return result.Substring(3);
        }

        protected void btTampilAnalisis_Click(object sender, EventArgs e)
        {
            if (this.hfTahun.Value != "" && this.hfMatakuliah.Value != "")
            {
                this.InitTabelTahunAnalisis();
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowScript", "ShowAnalisis();", true);
            }
        }

        private void InitTabelTahunAnalisis()
        {
            int index = this.ddlTahun.SelectedIndex;
            this.ddlTahunPilih.Items.Clear();
            int i = 0;
            while (i < 3)
            {
                if ((i + index) > this.ddlTahun.Items.Count - 1) break;
                this.ddlTahunPilih.Items.Add(this.ddlTahun.Items[i + index]);
                i++;
            }
            this.ddlTahunPilih.Items.Add(new ListItem("all", "all"));
        }

        protected void btTampilkanAnalisis_Click(object sender, EventArgs e)
        {
            if (this.ddlTahunPilih.SelectedValue != "all")
            {
                tahun = new List<string>();
                tahun.Add(this.ddlTahunPilih.SelectedValue);

                this.ha1 = new HitungAnalisis(this.ddlTahunPilih.SelectedValue, this.ddlPeriode.SelectedValue, this.ddlMatakuliah.SelectedValue, System.IO.Path.GetFullPath(Server.MapPath("~/data.txt")));
                this.ha1.Hitung();
            }
            else
            {
                tahun = new List<string>();
                foreach (ListItem item in this.ddlTahunPilih.Items)
                {
                    if (item.Value != "all")
                        tahun.Add(item.Value);
                }

                if (tahun.Count <= 3)
                {
                    this.ha1 = new HitungAnalisis(this.tahun[0], this.ddlPeriode.SelectedValue, this.ddlMatakuliah.SelectedValue, System.IO.Path.GetFullPath(Server.MapPath("~/data.txt")));
                    this.ha1.Hitung();
                }

                if (tahun.Count <= 3 && tahun.Count > 1)
                {
                    this.ha2 = new HitungAnalisis(this.tahun[1], this.ddlPeriode.SelectedValue, this.ddlMatakuliah.SelectedValue, System.IO.Path.GetFullPath(Server.MapPath("~/data.txt")));
                    this.ha2.Hitung();
                }


                if (tahun.Count <= 3 && tahun.Count > 2)
                {
                    this.ha3 = new HitungAnalisis(this.tahun[2], this.ddlPeriode.SelectedValue, this.ddlMatakuliah.SelectedValue, System.IO.Path.GetFullPath(Server.MapPath("~/data.txt")));
                    this.ha3.Hitung();
                }
            }

            this.TampilChartAnalisis();
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ChartTabel();", "", true);
            this.TampilTabelAnalisis();
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "TabelChart();", "", true);
            this.TampilAlertAnalisis();
        }

        private void TampilAlertAnalisis()
        {
            if (tahun.Count <= 3)
            {
                if (ha1.Sample > 0)
                {
                    mywarning1.Attributes.Remove("hidden");
                    lt1.Text = "Sampel di matakuliah " + this.ddlMatakuliah.SelectedItem.Text + " " + this.ddlPeriode.SelectedItem.Text + " " +  ha1.Tahun + " kurang " + ha1.Sample + " lagi.";
                }
            }

            if (tahun.Count <= 3 && tahun.Count > 1)
            {
                if (ha2.Sample > 0)
                {
                    mywarning2.Attributes.Remove("hidden");
                    lt2.Text = "Sampel di matakuliah " + this.ddlMatakuliah.SelectedItem.Text + " " + this.ddlPeriode.SelectedItem.Text + " " + ha2.Tahun + " kurang " + ha2.Sample + " lagi.";
                }
            }

            if (tahun.Count <= 3 && tahun.Count > 2)
            {
                if (ha3.Sample > 0)
                {
                    mywarning3.Attributes.Remove("hidden");
                    lt3.Text = "Sampel di matakuliah " + this.ddlMatakuliah.SelectedItem.Text + " " + this.ddlPeriode.SelectedItem.Text + " " +  ha3.Tahun + " kurang " + ha3.Sample + " lagi.";
                }
            }
        }

        private void TampilTabelAnalisis()
        {
            var data = new DataSet();
            var tahun = new List<string>();

            if (this.ddlTahunPilih.SelectedValue != "all")
            {
                tahun.Add(this.ddlTahunPilih.SelectedValue);
                multipleTahun = false;
            }
            else
            {
                foreach (ListItem item in this.ddlTahunPilih.Items)
                {
                    if (item.Value != "all")
                    {
                        tahun.Add(item.Value);
                    }
                }
                multipleTahun = true;
            }

            if (AnalisisModel.GetPengaruhSkor(this.hfMatakuliah.Value, tahun, this.hfPeriode.Value, out data))
            {
                int index = GetColumnIndexByName(this.gvPertanyaan, "tahun");

                if (index != -1)
                {
                    if (!multipleTahun)
                    {
                        //this.gvPertanyaan.Columns[2].Visible = false;
                        this.gvPertanyaan.Columns[index].Visible = false;
                        System.Diagnostics.Debug.WriteLine("SINGLE");
                    }
                    else
                    {
                        //this.gvPertanyaan.Columns[2].Visible = true;
                        this.gvPertanyaan.Columns[index].Visible = true;
                        System.Diagnostics.Debug.WriteLine("MULTIPLE");
                    }
                }

                this.gvPertanyaan.Visible = true;
                this.gvPertanyaan.DataSource = data.Tables[0];
                this.gvPertanyaan.DataBind();



                string script = "$('#AnalisisModal').modal('hide'); Init();";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowTabel", script, true);
            }
        }

        private int GetColumnIndexByName(GridView grid, string name)
        {
            foreach (DataControlField col in grid.Columns)
            {
                if (col.HeaderText.ToLower().Trim() == name.ToLower().Trim())
                {
                    return grid.Columns.IndexOf(col);
                }
            }

            return -1;
        }

        private void TampilChartAnalisis()
        {
            var data = new DataSet();
            var tahun = new List<string>();

            if (this.ddlTahunPilih.SelectedValue != "all")
            {
                tahun.Add(this.ddlTahunPilih.SelectedValue);
                if (AnalisisModel.GetPengaruhSkor(this.hfMatakuliah.Value, tahun, this.hfPeriode.Value, out data))
                {
                    var pengaruh = new List<double>();
                    var skor = new List<double>();
                    var kategori = new List<string>();

                    foreach (DataRow item in data.Tables[0].Rows)
                    {
                        pengaruh.Add(double.Parse(item[1].ToString()));
                        skor.Add(double.Parse(item[2].ToString()));
                        kategori.Add(String.Format("{0} {1}", item[0], item[3]));
                    }

                    string script = "var chart = c3.generate({ bindto: '#chart-query0', data: { columns: [  ['skor'" + this.PrintRerata(skor) + "], ['pengaruh'" + this.PrintRerata(pengaruh) + "] ], type: 'bar' }, bar: { width: { ratio: 0.5 } }, axis: { x: { type: 'category',tick: { rotate: 75 }, height: 130, categories: [" + this.PrintCategory(kategori) + "] }   } }); $('#AnalisisModal').modal('hide'); $('#bagianchart0').removeAttr('hidden'); $('#bagianchart1').attr('hidden', 'hidden'); $('#bagianchart2').attr('hidden', 'hidden');";


                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowChartSingle", script, true);
                }
            }
            else
            {
                foreach (ListItem item in this.ddlTahunPilih.Items)
                {
                    if (item.Value != "all")
                    {
                        tahun.Add(item.Value);
                    }
                }

                for (int i = 0; i < tahun.Count; i++)
                {
                    if (AnalisisModel.GetPengaruhSkor(this.hfMatakuliah.Value, new List<string> { tahun[i] }, this.hfPeriode.Value, out data))
                    {
                        var pengaruh = new List<double>();
                        var skor = new List<double>();
                        var kategori = new List<string>();

                        foreach (DataRow item in data.Tables[0].Rows)
                        {
                            pengaruh.Add(double.Parse(item[1].ToString()));
                            skor.Add(double.Parse(item[2].ToString()));
                            kategori.Add(String.Format("{0} {1}", item[0], item[3]));
                        }

                        string script = "var chart = c3.generate({ bindto: '#chart-query" + i + "', data: { columns: [  ['skor'" + this.PrintRerata(skor) + "], ['pengaruh'" + this.PrintRerata(pengaruh) + "] ], type: 'bar' }, bar: { width: { ratio: 0.5 } }, axis: { x: { type: 'category',tick: { rotate: 75 }, height: 130, categories: [" + this.PrintCategory(kategori) + "] }   } }); $('#AnalisisModal').modal('hide'); $('#bagianchart" + i + "').removeAttr('hidden');";


                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowChart" + i, script, true);
                    }
                }
            }


        }

        protected void gvPertanyaan_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void lnkLogout_Click(object sender, EventArgs e)
        {
            Session["id_role"] = "";
            Session["username"] = "";
            CheckSession();
        }

        protected void gvPertanyaan_DataBound(object sender, EventArgs e)
        {
            //if (Session["id_role"].ToString() != "RL02")
            //{
            //    gvPertanyaan.Columns[4].Visible = true;
            //    gvPertanyaan.Columns[6].Visible = true;
            //}
            

            switch (this.tahun.Count)
            {
                case 1:
                    for (int i = 0; i < gvPertanyaan.Rows.Count; i = i + 1)
                    {
                        if (Session["id_role"].ToString() == "RL02")
                        {
                            if (this.ha1.NonValid.Contains(i))
                            {
                                if (!gvPertanyaan.Rows[i].Cells[1].Text.Contains("TIDAK VALID"))
                                    gvPertanyaan.Rows[i].Cells[1].Text += " <span class='label label-warning'>TIDAK VALID</span>";
                            }
                            if (this.ha1.NonReliabel.Contains(i))
                            {
                                if (!gvPertanyaan.Rows[i].Cells[1].Text.Contains("TIDAK RELIABEL"))
                                    gvPertanyaan.Rows[i].Cells[1].Text += " <span class='label label-danger'>TIDAK RELIABEL</span>";
                            }
                            gvPertanyaan.Rows[i].Cells[6].Text = ha1.GetKeteranganSkor(i);
                            gvPertanyaan.Rows[i].Cells[4].Text = ha1.GetKeteranganPengaruh(i);
                        }
                        else
                        {
                            if (this.ha1.NonValid.Contains(i) || this.ha1.NonReliabel.Contains(i))
                            {
                                gvPertanyaan.Rows[i].Visible = false;
                            }
                            else
                            {
                                gvPertanyaan.Rows[i].Cells[6].Text = ha1.GetKeteranganSkor(i);
                                gvPertanyaan.Rows[i].Cells[4].Text = ha1.GetKeteranganPengaruh(i);
                            }
                        }
                    }
                    break;
                case 2:
                    for (int i = 0; i < gvPertanyaan.Rows.Count; i = i + 2)
                    {
                        if (Session["id_role"].ToString() == "RL02")
                        {
                            if (this.ha1.NonValid.Contains(i / 2))
                            {
                                if (!gvPertanyaan.Rows[i].Cells[1].Text.Contains("TIDAK VALID"))
                                    gvPertanyaan.Rows[i].Cells[1].Text += " <span class='label label-warning'>TIDAK VALID</span>";
                            }
                            if (this.ha2.NonValid.Contains(i / 2))
                            {
                                if (!gvPertanyaan.Rows[i + 1].Cells[1].Text.Contains("TIDAK VALID"))
                                    gvPertanyaan.Rows[i + 1].Cells[1].Text += " <span class='label label-warning'>TIDAK VALID</span>";
                            }
                            if (this.ha1.NonReliabel.Contains(i / 2))
                            {
                                if (!gvPertanyaan.Rows[i].Cells[1].Text.Contains("TIDAK RELIABEL"))
                                    gvPertanyaan.Rows[i].Cells[1].Text += " <span class='label label-danger'>TIDAK RELIABEL</span>";
                            }
                            if (this.ha2.NonReliabel.Contains(i / 2))
                            {
                                if (!gvPertanyaan.Rows[i + 1].Cells[1].Text.Contains("TIDAK RELIABEL"))
                                    gvPertanyaan.Rows[i + 1].Cells[1].Text += " <span class='label label-danger'>TIDAK RELIABEL</span>";
                            }
                            gvPertanyaan.Rows[i].Cells[6].Text = ha1.GetKeteranganSkor(i / 2);
                            gvPertanyaan.Rows[i].Cells[4].Text = ha1.GetKeteranganPengaruh(i / 2);
                            gvPertanyaan.Rows[i + 1].Cells[6].Text = ha2.GetKeteranganSkor(i / 2);
                            gvPertanyaan.Rows[i + 1].Cells[4].Text = ha2.GetKeteranganPengaruh(i / 2);
                        }
                        else
                        {
                            if (this.ha1.NonValid.Contains(i / 2) || this.ha1.NonReliabel.Contains(i / 2))
                            {
                                gvPertanyaan.Rows[i].Visible = false;
                            }
                            else
                            {
                                gvPertanyaan.Rows[i].Cells[6].Text = ha1.GetKeteranganSkor(i / 2);
                                gvPertanyaan.Rows[i].Cells[4].Text = ha1.GetKeteranganPengaruh(i / 2);
                            }
                            if (this.ha2.NonValid.Contains(i / 2) || this.ha2.NonReliabel.Contains(i / 2))
                            {
                                gvPertanyaan.Rows[i + 1].Visible = false;
                            }
                            else
                            {
                                gvPertanyaan.Rows[i + 1].Cells[6].Text = ha2.GetKeteranganSkor(i / 2);
                                gvPertanyaan.Rows[i + 1].Cells[4].Text = ha2.GetKeteranganPengaruh(i / 2);
                            }
                        }
                    }
                    break;
                case 3:
                    for (int i = 0; i < gvPertanyaan.Rows.Count; i = i + 3)
                    {
                        if (Session["id_role"].ToString() == "RL02")
                        {
                            if (this.ha1.NonValid.Contains(i / 3))
                            {
                                if (!gvPertanyaan.Rows[i].Cells[1].Text.Contains("TIDAK VALID"))
                                    gvPertanyaan.Rows[i].Cells[1].Text += " <span class='label label-warning'>TIDAK VALID</span>";
                            }
                            if (this.ha2.NonValid.Contains(i / 3))
                            {
                                if (!gvPertanyaan.Rows[i + 1].Cells[1].Text.Contains("TIDAK VALID"))
                                    gvPertanyaan.Rows[i + 1].Cells[1].Text += " <span class='label label-warning'>TIDAK VALID</span>";
                            }
                            if (this.ha3.NonValid.Contains(i / 3))
                            {
                                if (!gvPertanyaan.Rows[i + 2].Cells[1].Text.Contains("TIDAK VALID"))
                                    gvPertanyaan.Rows[i + 2].Cells[1].Text += " <span class='label label-warning'>TIDAK VALID</span>";
                            }
                            if (this.ha1.NonReliabel.Contains(i / 3))
                            {
                                if (!gvPertanyaan.Rows[i].Cells[1].Text.Contains("TIDAK RELIABEL"))
                                    gvPertanyaan.Rows[i].Cells[1].Text += " <span class='label label-danger'>TIDAK RELIABEL</span>";
                            }
                            if (this.ha2.NonReliabel.Contains(i / 3))
                            {
                                if (!gvPertanyaan.Rows[i + 1].Cells[1].Text.Contains("TIDAK RELIABEL"))
                                    gvPertanyaan.Rows[i + 1].Cells[1].Text += " <span class='label label-danger'>TIDAK RELIABEL</span>";
                            }
                            if (this.ha3.NonReliabel.Contains(i / 3))
                            {
                                if (!gvPertanyaan.Rows[i + 2].Cells[1].Text.Contains("TIDAK RELIABEL"))
                                    gvPertanyaan.Rows[i + 2].Cells[1].Text += " <span class='label label-danger'>TIDAK RELIABEL</span>";
                            }
                            gvPertanyaan.Rows[i].Cells[6].Text = ha1.GetKeteranganSkor(i / 3);
                            gvPertanyaan.Rows[i].Cells[4].Text = ha1.GetKeteranganPengaruh(i / 3);
                            gvPertanyaan.Rows[i + 1].Cells[6].Text = ha2.GetKeteranganSkor(i / 3);
                            gvPertanyaan.Rows[i + 1].Cells[4].Text = ha2.GetKeteranganPengaruh(i / 3);
                            gvPertanyaan.Rows[i + 2].Cells[6].Text = ha3.GetKeteranganSkor(i / 3);
                            gvPertanyaan.Rows[i + 2].Cells[4].Text = ha3.GetKeteranganPengaruh(i / 3);
                        }
                        else
                        {
                            if (this.ha1.NonValid.Contains(i / 3) || this.ha1.NonReliabel.Contains(i / 3))
                            {
                                gvPertanyaan.Rows[i].Visible = false;
                            }
                            else
                            {
                                gvPertanyaan.Rows[i].Cells[6].Text = ha1.GetKeteranganSkor(i / 3);
                                gvPertanyaan.Rows[i].Cells[4].Text = ha1.GetKeteranganPengaruh(i / 3);
                            }
                            if (this.ha2.NonValid.Contains(i / 3) || this.ha2.NonReliabel.Contains(i / 3))
                            {
                                gvPertanyaan.Rows[i + 1].Visible = false;
                            }
                            else
                            {
                                gvPertanyaan.Rows[i + 1].Cells[6].Text = ha2.GetKeteranganSkor(i / 3);
                                gvPertanyaan.Rows[i + 1].Cells[4].Text = ha2.GetKeteranganPengaruh(i / 3);
                            }
                            if (this.ha3.NonValid.Contains(i / 3) || this.ha3.NonReliabel.Contains(i / 3))
                            {
                                gvPertanyaan.Rows[i + 2].Visible = false;
                            }
                            else
                            {
                                gvPertanyaan.Rows[i + 2].Cells[6].Text = ha3.GetKeteranganSkor(i / 3);
                                gvPertanyaan.Rows[i + 2].Cells[4].Text = ha3.GetKeteranganPengaruh(i / 3);
                            }
                        }
                    }
                    break;
                default:
                    break;
            }

            #region Gabung
            int num = 1;
            for (int rowIndex = gvPertanyaan.Rows.Count - 2; rowIndex >= 0; rowIndex--)
            {
                GridViewRow gvRow = gvPertanyaan.Rows[rowIndex];
                GridViewRow gvPreviousRow = gvPertanyaan.Rows[rowIndex + 1];

                if (gvPreviousRow.Visible == false || gvRow.Visible == false) continue;

                if (gvRow.Cells[1].Text == gvPreviousRow.Cells[1].Text)
                {
                    gvPreviousRow.Cells[0].Text = "";
                    if (gvPreviousRow.Cells[1].RowSpan < 2)
                    {
                        gvRow.Cells[1].RowSpan = 2;
                    }
                    else
                    {
                        gvRow.Cells[1].RowSpan = gvPreviousRow.Cells[1].RowSpan + 1;
                    }
                    gvPreviousRow.Cells[1].Visible = false;
                }


            }


            for (int i = 0; i < gvPertanyaan.Rows.Count; i++)
            {
                //if (Session["id_role"].ToString() == "RL02")
                //{
                //    if (this.pengecualian.Contains(i))
                //    {
                //        gvPertanyaan.Rows[i].Cells[1].Text += " <span class='label label-warning'>TIDAK VALID</span>";
                //        gvPertanyaan.Rows[i].Cells[1].Text += " <span class='label label-danger'>TIDAK RELIABEL</span>";
                //    }
                //    else if (!this.pengecualian.Contains(i) && this.hasilReliabilitas < 0.6)
                //    {
                //        gvPertanyaan.Rows[i].Cells[1].Text += " <span class='label label-danger'>TIDAK RELIABEL</span>";
                //    } 
                //}

                if (gvPertanyaan.Rows[i].Cells[1].Visible == true)
                {
                    gvPertanyaan.Rows[i].Cells[0].Text = num.ToString() + ".";
                    num++;

                    //if (gvPertanyaan.Rows[i].Cells[3].Text == "0" || gvPertanyaan.Rows[i].Cells[5].Text == "0")
                    //{
                    //    gvPertanyaan.Rows[i].Visible = false;
                    //    if (gvPertanyaan.Rows[i - 1].Cells[1].Visible == false)
                    //        gvPertanyaan.Rows[i].Cells[1].Visible = true;
                    //}
                }


                gvPertanyaan.Rows[i].Cells[3].Text = String.Format("{0:F2}", double.Parse(gvPertanyaan.Rows[i].Cells[3].Text));
                gvPertanyaan.Rows[i].Cells[5].Text = String.Format("{0:F2}", double.Parse(gvPertanyaan.Rows[i].Cells[5].Text));
            }
            #endregion



            //if (int.Parse(Session["id_role"].ToString()) > 3)
            //{
            //    double sample = this.totalPopulasi / ((1.0 * this.totalPopulasi) * Math.Pow(0.05, 2.0));

            //}

            
        }

        //#region BagianHitung

        //private DataSet hasilQueryData;
        //private DataSet hasilQueryPertanyaan;
        //private DataSet hasilQueryFuzzy;

        //private int totalPopulasi;
        //private int totalPertanyaan;

        //private int[,] dataInt;
        //private double[,] dataDouble;

        //private double[] hasilValiditas;
        //private double hasilReliabilitas;
        //private double[] hasilSkor;
        //private double[] hasilFuzzy;

        //private double[] nilaiAkhir;
        //private int[] kehadiran;

        //private List<int> pengecualian;

        //private void HitungPertahun()
        //{
        //    QueryPertanyaan();
        //    QueryNilai();
        //    QueryFuzzy();

        //    RTabel.Instance.SetPath(System.IO.Path.GetFullPath(Server.MapPath("~/data.txt")));

        //    HitungValiditas();
        //    HitungReliabilitas();
        //    HitungSkor();
        //    HitungFuzzy();

        //    Simpan();
        //}

        //private void QueryPertanyaan()
        //{
        //    this.hasilQueryPertanyaan = new DataSet();

        //    if (PerhitunganModel.GetTabelPertanyaan(this.ddlMatakuliah.SelectedValue, this.ddlTahunPilih.SelectedValue, this.ddlPeriode.SelectedValue, out this.hasilQueryPertanyaan))
        //    {
        //        var items = this.hasilQueryPertanyaan.Tables[0].Rows;

        //        /* mendapatkan total pertanyaan */
        //        this.totalPertanyaan = items.Count;

        //        this.hasilValiditas = new double[this.totalPertanyaan];
        //        this.hasilSkor = new double[this.totalPertanyaan];
        //    }
        //}

        //private void Simpan()
        //{
        //    for (int i = 0; i < this.totalPertanyaan; i++)
        //    {
        //        var analisis = new AnalisisObject();
        //        analisis.Tahun = this.ddlTahun.SelectedValue;
        //        analisis.Periode = this.ddlPeriode.SelectedValue;
        //        analisis.Matakuliah = this.ddlMatakuliah.SelectedValue;
        //        analisis.Pertanyaan = this.hasilQueryPertanyaan.Tables[0].Rows[i][0].ToString();
        //        analisis.Validitas = this.hasilValiditas[i];
        //        if (this.pengecualian.Contains(i))
        //            analisis.Reliabilitas = 0.0;
        //        else
        //            analisis.Reliabilitas = this.hasilReliabilitas;
        //        analisis.Pengaruh = this.hasilFuzzy[i];
        //        analisis.Skor = this.hasilSkor[i];

        //        AnalisisModel.Perbarui(analisis);
        //    }
        //}

        //private void QueryFuzzy()
        //{
        //    this.nilaiAkhir = new double[this.totalPopulasi];
        //    this.kehadiran = new int[this.totalPopulasi];

        //    this.hasilFuzzy = new double[this.totalPertanyaan];

        //    if (PerhitunganModel.GetNilaiAkhir(this.ddlMatakuliah.SelectedValue, this.ddlTahunPilih.SelectedValue, out this.hasilQueryFuzzy))
        //    {
        //        for (int i = 0; i < hasilQueryFuzzy.Tables[0].Rows.Count; i++)
        //        {
        //            var item = hasilQueryFuzzy.Tables[0].Rows[i];
        //            this.kehadiran[i] = Convert.ToInt32(item[0].ToString());
        //            this.nilaiAkhir[i] = Convert.ToDouble(item[1].ToString());
        //        }
        //    }
        //}

        //private void HitungFuzzy()
        //{
        //    var A = new List<double>();
        //    var AKehadiran = new List<double>();

        //    var fuzzyCalc = new FuzzyCalculation(this.totalPopulasi, this.totalPertanyaan);
        //    fuzzyCalc.InitKehadiran(this.kehadiran);
        //    fuzzyCalc.InitNilai(this.nilaiAkhir);
        //    fuzzyCalc.InitA(this.dataDouble);
        //    fuzzyCalc.Calculate(out A, out AKehadiran);

        //    for (int i = 0; i < A.Count; i++)
        //    {
        //        this.hasilFuzzy[i] = A[i];
        //    }
        //}

        //private void HitungSkor()
        //{
        //    var skor = new Skor(this.totalPopulasi, this.totalPertanyaan, this.dataInt, 4);
        //    var hasil = skor.HitungSkor();

        //    for (int i = 0; i < hasil.Count; i++)
        //    {
        //        this.hasilSkor[i] = hasil[i];
        //    }
        //}

        //private void HitungReliabilitas()
        //{
        //    var koefisien = new KoefisienReliabilitas(this.totalPopulasi, this.totalPertanyaan, this.dataInt);
        //    this.hasilReliabilitas = koefisien.HitungKoefisienReliabilitas(pengecualian);
        //}

        //private void HitungValiditas()
        //{
        //    var koefisien = new KoefisienKorelasi[this.totalPertanyaan];
        //    var Y = new List<int>();
        //    var Xs = new List<int>[this.totalPertanyaan];

        //    this.pengecualian = new List<int>();

        //    // init masing-masing X
        //    for (int i = 0; i < this.totalPertanyaan; i++)
        //    {
        //        Xs[i] = new List<int>();
        //    }

        //    // dapatkan Y
        //    for (int i = 0; i < this.totalPopulasi; i++)
        //    {
        //        int y = 0;
        //        for (int j = 0; j < this.totalPertanyaan; j++)
        //        {
        //            y += dataInt[i, j];
        //        }

        //        Y.Add(y);
        //    }

        //    for (int i = 0; i < this.totalPertanyaan; i++)
        //    {
        //        koefisien[i] = new KoefisienKorelasi();
        //        koefisien[i].UkuranPopulasi = this.totalPopulasi;

        //        for (int j = 0; j < totalPopulasi; j++)
        //        {
        //            Xs[i].Add(this.dataInt[j, i]);
        //        }

        //        foreach (var item in Xs[i])
        //            koefisien[i].TambahSkorItem(item);

        //        foreach (var item in Y)
        //            koefisien[i].TambahSkorTotalItem(item);

        //        this.hasilValiditas[i] = koefisien[i].HitungKoefisienKorelasi();
        //        if (this.hasilValiditas[i] < RTabel.Instance[this.totalPopulasi - 2])
        //        {
        //            this.pengecualian.Add(i);
        //        }
        //    }
        //}

        //private void QueryNilai()
        //{
        //    this.hasilQueryData = new DataSet();

        //    if (PerhitunganModel.GetTabelNilai(this.ddlMatakuliah.SelectedValue, this.ddlTahunPilih.SelectedValue, out this.hasilQueryData))
        //    {
        //        var items = this.hasilQueryData.Tables[0].Rows;

        //        /* mendapatkan total populasi */
        //        this.totalPopulasi = items.Count;

        //        this.dataInt = new int[this.totalPopulasi, this.totalPertanyaan];
        //        this.dataDouble = new double[this.totalPopulasi, this.totalPertanyaan];

        //        /* pengisian jawaban */
        //        for (int i = 0; i < this.totalPopulasi; i++)
        //        {
        //            string[] num = items[i][4].ToString().Split(new char[] { ',' });

        //            for (int j = 0; j < num.Length; j++)
        //            {
        //                this.dataInt[i, j] = int.Parse(num[j]);
        //                this.dataDouble[i, j] = double.Parse(num[j]);
        //            }
        //        }
        //    }
        //}



        //#endregion

    }
}